package com.neuedu.javase.tools.wrapper;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

/***
 * 项目名称:  MyJavaSE 
 * 包：  com.neuedu.javase.tools.wrapper 
 * date:   2025/10/29   14:29  
 * 描述:  
 *  @author : 张金山
 *  作者博客:   https://blog.fulfill.com.cn
 */
public class Student {

    private String name;

    //默认值 0
    // private int age;

    //为了避免出现  默认为0 的情况 ，可以使用 Integer 类型   引用类型 -->  默认值 是 null
    private Integer age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }


    public static void main(String[] args) {
        //构造对象
        Student stu1 = new Student();
        stu1.name = "李四";
        // stu1.age = 0;


        Student stu2 = new Student("张三",0);


        System.out.println("stu1 = " + stu1);
        System.out.println("stu2 = " + stu2);

        /**
         * stu1 = Student{name='李四', age=0}
         * stu2 = Student{name='张三', age=0}
         */


        /**
         *   所有的基础类型，都有对应的包装类
         *
         *
         *   基本类型	包装类
         * boolean	    Boolean

         * char 	    Character
         * double	    Double
         * float	     Float

         *  byte	      Byte
         * short	    Short
         * int	        Integer
         * long	        Long
         */

        // 创建包装类型的 对象
        Integer intObj = new Integer(100);
        Byte byteObj = new Byte("15");

        System.out.println("intObj = " + intObj);
        System.out.println("byteObj = " + byteObj);

        //使用valueOf  静态方法 构造对象
        Integer intObj2 = Integer.valueOf(200);
        Byte byteObj2 = Byte.valueOf((byte)15);

        System.out.println("intObj2 = " + intObj2);
        System.out.println("byteObj2 = " + byteObj2);





        // 基础类型  转换成   包装类型
        //基础类型
        boolean  baseBooleanData = false;
        // 1 使用构造方法 将基础类型包装成 包装类型
        Boolean booleanObj =  new Boolean(baseBooleanData);  //false
        System.out.println("booleanObj = " + booleanObj);

        // 2 使用静态方法   XXXXX。valueOf(xxxx)
        double baseDoubleData = 3.14;
        Double doubleObj = Double.valueOf(baseDoubleData);
        System.out.println("doubleObj = " + doubleObj);


        // 包装类型  转换成     基础类型     xxxxValue
        Boolean booleanObj2 =  new Boolean(true);  //false
        boolean baseBooleanData2 = booleanObj2.booleanValue();





        //  包装类型  转换成     字符串 toString
        Integer intObj3 = new Integer(3000);
        String strFromInteger = intObj3.toString();


        //  字符串 转换成   包装类型     xxx.valueOf（String)
        // String  doubleStr = "3.14fgggaasfddsafdsafdsafdsadsasa";  //会报错，出现了非数字的表示形式
        /**
         * Exception in thread "main" java.lang.NumberFormatException: For input string: "3.14fgggaasfddsafdsafdsafdsadsasa"
         * 	at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
         * 	at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
         * 	at java.base/java.lang.Double.parseDouble(Double.java:651)
         * 	at java.base/java.lang.Double.valueOf(Double.java:614)
         * 	at com.neuedu.javase.tools.wrapper.Student.main(Student.java:120)
         */
        String  doubleStr = "3.14";    //  √
        Double doubleObj3 = Double.valueOf(doubleStr);
        int sourceInt = doubleObj3.intValue();


        System.out.println("(sourceInt * 2 ) = " + (sourceInt * 2));





    //     1.5+  自动装箱（基础类型-->包装类 ）   自动拆箱（包装类  -> 基础类形)
        int normalIntValue = 15;

        //自动装箱
        Integer warpperObj = normalIntValue;    //1.4- 会报错   1.5+ok
        //自动拆箱
        int normalIntValue2 = warpperObj;


        Double pi = 3.14;
        double doubelPi = pi;



        printInteger(10);


    }



    public static void printInteger(Integer value){
        System.out.println(value);



        List<Integer> list = new ArrayList();

        //装箱
        list.add(100);
        list.add(300);
        list.add(200);

        System.out.println("list.get(0).getClass() = " + list.get(0).getClass());

        //拆箱
        System.out.println("(list.get(0) + list.get(1)) = " + (list.get(0) + list.get(1)));




        Short b1 = 20;
        Short b2 = 20;
        System.out.println("(b1 == b2) = " + (b1 == b2));   //true


        // -128   +127 会存储在常量区
        Integer b3 = -129;
        Integer b4 = -129;
        System.out.println("(b3 == b4) = " + (b3 == b4));   //false

    }




}
